<template>
  <el-dialog @close="closeDialog" :title="form.id ? $t('common.editBtn') : $t('common.addBtn')" v-model="visible" :close-on-click-modal="false"
    draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item prop="shopId" :label="$t('ProdGroupReference.shopId')">
            <ShopSelect v-model="form.shopId" />

          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="groupId" :label="$t('ProdGroupReference.groupId')">
            <el-input v-model="form.groupId" :placeholder="$t('ProdGroupReference.inputGroupIdTip')" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="prodId" :label="$t('ProdGroupReference.prodId')">
            <el-input v-model="form.prodId" :placeholder="$t('ProdGroupReference.inputProdIdTip')" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="state" :label="$t('ProdGroupReference.state')">

            <el-switch :active-value="1" v-model="form.state" :inactive-value="0"></el-switch>
            <!-- <el-input v-model="form.state" :placeholder="$t('ProdGroupReference.inputStateTip')" clearable
              show-word-limit /> -->
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ProdGroupReferenceDialog">
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/bizs/ProdGroupReference'
const emit = defineEmits(['refresh']);

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 定义字典
import { useDict } from '/@/hooks/dict';
const ShopSelect = defineAsyncComponent(() => import("/@/components/ShopSelect/index.vue"))

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const dataFormRef = ref();

// 提交表单数据
const form = reactive({
  id: '',
  shopId: '',
  groupId: '',
  prodId: '',
  state: '',
});

// 定义校验规则
import { rule } from '/@/utils/validate';
const dataRules = ref({
  shopId: [{ required: false, message: t('ProdGroupReference.inputShopIdTip'), trigger: 'blur' }],
  groupId: [{ required: false, message: t('ProdGroupReference.inputGroupIdTip'), trigger: 'blur' }],
  prodId: [{ required: false, message: t('ProdGroupReference.inputProdIdTip'), trigger: 'blur' }],
  state: [{ required: false, message: t('ProdGroupReference.inputStateTip'), trigger: 'blur' }],
})


// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取信息
  if (id) {
    form.id = id
    initFormData(id)
  }
};

// 初始化表单
const initFormData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 提交表单
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('common.editSuccessText') : t('common.addSuccessText'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 暴露变量
defineExpose({
  openDialog
});
</script>
